package com.ficus.road.maintain.core.model.smartnew;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.ficus.road.maintain.core.model.smartnew.annotation.Excel;
import com.google.common.base.Joiner;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Field;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/7/13 下午4:57
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SmartDecisionExcelVO extends SmartDecisionExcel {
    @Id
    private String id;
    @Excel("道路名称")
    private String roadName;
    @Excel("车道方向")
    private String direction;
    /**
     * 大致方向
     */
    private String generalDirection;
    @Excel(value = "起点桩号", type = CellType.NUMERIC)
    private Double startStake;
    @Excel(value = "止点桩号", type = CellType.NUMERIC)
    private Double endStake;
    @Excel(value = "路段长度", type = CellType.NUMERIC)
    private Double length;
    @Excel("道路等级")
    private String level;
    @Excel("道路类型")
    private String roadCharacter;
    @Excel("需求来源")
    private String source;
    @Field("PCI")
    @Excel(value = "PCI", type = CellType.NUMERIC)
    private Double PCI;
    @Field("RQI")
    @Excel(value = "RQI", type = CellType.NUMERIC)
    private Double RQI;
    @Field("RDI")
    @Excel(value = "RDI", type = CellType.NUMERIC)
    private Double RDI;
    @Excel(value = "SRI", type = CellType.NUMERIC)
    @Field("SRI")
    private Double SRI;
    @Field("PSSI")
    @Excel(value = "PSSI", type = CellType.NUMERIC)
    @JsonProperty("PSSI")
    private Double PSSI;
    @Excel(value = "合并PSSI", type = CellType.NUMERIC)
    @JsonProperty("PSSIMerge")
    @Field("PSSIMerge")
    private Double PSSIMerge;
    @Excel(value = "养护面积", type = CellType.NUMERIC)
    private Double area;
    @Excel(value = "上一次大中修时间", type = CellType.NUMERIC)
    private Double lastOverhaulYear;
    @Excel(value = "新/改建/接管时间", type = CellType.NUMERIC)
    private Double newBuildYear;
    @Excel(value = "是否为最终结果", type = CellType.STRING)
    private String isFinalResult;
    /**
     * 组id
     */
    private int groupId;
    /**
     * 用于排序
     */
    private int order;

    public SmartDecisionExcelVO(SmartDecisionExcelVO vo) {
        this.id = vo.getId();
        this.roadName = vo.getRoadName();
        this.direction = vo.getDirection();
        this.startStake = vo.getStartStake();
        this.endStake = vo.getEndStake();
        this.length = vo.getLength();
        this.level = vo.getLevel();
        this.roadCharacter = vo.getRoadCharacter();
        this.source = vo.getSource();
        this.PCI = vo.getPCI();
        this.RQI = vo.getRQI();
        this.RDI = vo.getRDI();
        this.SRI = vo.getSRI();
        this.PSSI = vo.getPSSI();
        this.PSSIMerge = vo.getPSSIMerge();
        this.area = vo.getArea();
        this.lastOverhaulYear = vo.getLastOverhaulYear();
        this.generalDirection = vo.getGeneralDirection();
        this.groupId = vo.getGroupId();
        this.order = vo.getOrder();
        this.isFinalResult = vo.getIsFinalResult();
        this.newBuildYear = vo.getNewBuildYear();
    }

    public SmartDecisionExcelVO(String id) {
        this.id = id;
        this.roadName = id;
        this.order = 0;
        this.groupId = -1;
    }

    public SmartDecisionExcelVO() {
    }

    public static SmartDecisionExcelVO roadId(SmartDecisionExcelVO vo) {
        if (ObjectUtils.isEmpty(vo.getRoadName()) || ObjectUtils.isEmpty(vo.getDirection()) || ObjectUtils.isEmpty(vo.getStartStake()) || ObjectUtils.isEmpty(vo.getEndStake())) {
            return null;
        }
        List<String> list = Arrays.asList(vo.getRoadName(), vo.getDirection(), Double.toString(vo.getStartStake()), Double.toString(vo.getEndStake()));
        Joiner joiner = Joiner.on("_");
        String id = joiner.join(list);
        vo.setId(id);
        return vo;
    }

    public static SmartDecisionExcelVO completeAttr(SmartDecisionExcelVO vo) {
        // area
        Double length = vo.getLength();
        if (ObjectUtils.isNotEmpty(length)) {
            vo.setArea(length * 3.75 * 1000);
        }
        // direction
        if (ObjectUtils.isNotEmpty(vo.getDirection())) {
            String generalDirection = vo.getDirection().substring(0, 2);
            vo.setGeneralDirection(generalDirection);
        }
        // order weight
        if (ObjectUtils.isNotEmpty(vo.getSource())) {
            switch (vo.getSource()) {
                case "重点区域":
                    vo.setOrder(1);
                    break;
                case "街镇需求":
                    vo.setOrder(2);
                    break;
                case "路况需求":
                    vo.setOrder(3);
                    break;
                default:
                    vo.setOrder(4);
                    break;
            }
        } else {
            vo.setOrder(4);
        }
        return vo;
    }
}
